﻿using System;
using System.Data;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class WgjCk : PageBase
    {
        protected override bool IsAccess()
        {
            return (IsAccess("CpSjBj.aspx") | IsAccess("CpSjCx.aspx") | IsAccess("WgjCx.aspx"));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                TextId.Value = Request.QueryString["WgjId"];
                BindData();
            }
        }

        private void BindData()
        {
            if ((TextId.Value.Trim() != string.Empty) && BGlobal.IsNumeric(TextId.Value.Trim()))
            {
                DataSet ds = BWgjCx.GetWgjCxByWgjId(Convert.ToInt32(TextId.Value.Trim()));
                if ((ds != null) && (ds.Tables[0].Rows.Count == 1))
                {
                    PanelEdit.Visible = true;
                    TextDh.Text = ds.Tables[0].Rows[0]["代号"].ToString();
                    TextLb.Text = ds.Tables[0].Rows[0]["类别"].ToString();
                    TextMc.Text = ds.Tables[0].Rows[0]["名称"].ToString();
                    TextGgxh.Text = ds.Tables[0].Rows[0]["规格型号"].ToString();
                    TextDw.Text = ds.Tables[0].Rows[0]["单位"].ToString();
                    TextPpcj.Text = ds.Tables[0].Rows[0]["品牌厂家"].ToString();
                    TextBz.Text = ds.Tables[0].Rows[0]["备注"].ToString();
                    TextBjr.Text = ds.Tables[0].Rows[0]["编辑人"].ToString();
                    TextBjsj.Text = ds.Tables[0].Rows[0]["编辑时间"].ToString();
                    BindWgjWdData(TextDh.Text.Trim());
                }
            }
        }

        private void BindWgjWdData(string sWgjDh)
        {
            DataSet ds = BWgjBj.GetWgjWdByWgjDh(sWgjDh);
            if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
            {
                PanelEditWd.Visible = true;
            }
            GridWgjWd.DataSource = ds;
            GridWgjWd.DataBind();
            if (!IsAccess("零件及购件文档下载") && !IsAccess("WgjCx.aspx"))
            {
                for (int i = 0; i < GridWgjWd.Rows.Count; i++)
                {
                    GridWgjWd.Rows[i].Cells[5].Controls[1].Visible = false;
                }
            }
        }

        protected void GridWgjWd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DownloadDoc")
            {
                Response.Redirect("DownloadFile.aspx?id=" + BGlobal.Encrypt(string.Format("{0}|{1}.{2}", GridWgjWd.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[0], GridWgjWd.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].Text.Trim(), BGlobal.HexToString(GridWgjWd.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[0].ToString().Split('_')[1])), "Download"));
            }
        }
    }
}